﻿using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using CommonClass;

public class FolderManager : EditorWindow
{
    /// <summary>
    /// <para>文件夹信息元组</para>
    /// <para>如果需要后续修改，直接在这里添加即可，不要修改其他参数</para> 
    /// <para>元组第一个数据是是否被创建，第二个为文件夹的名称，第三个为文件夹的简介</para>
    /// <para>目前只支持一级目录的创建</para>
    /// </summary>
    private List<Tuple<bool, string, string>> customFolderInfo = new List<Tuple<bool, string, string>> {
        new Tuple<bool, string, string>(false,"_Documents","游戏中的文档文件夹"),
        new Tuple<bool, string, string>(false,"_Scenes","游戏中存放场景的文件夹"),
        new Tuple<bool, string, string>(false,"_Scripts","游戏中存放脚本的文件夹"),
        new Tuple<bool, string, string>(false,"_ArtWork","游戏中存放美术资源的文件夹"),
        new Tuple<bool, string, string>(false,"_Effect","游戏中特效资源文件夹"),
        new Tuple<bool, string, string>(false,"_Prefab","游戏中预制体存放文件夹"),
    };
    private List<Tuple<bool, string, string>> standardFolderInfo = new List<Tuple<bool, string, string>> {
        new Tuple<bool, string, string>(false,"Editor","脚本也只能在编辑时使用。一般会把一些工具类的脚本放在这里，或者是一些编辑时用的DLL。"),
        new Tuple<bool, string, string>(false,"Editor Default Resources","不会被打到最终发布包里，仅仅用于开发时使用。"),
        new Tuple<bool, string, string>(false,"Gizmos","OnDrawGizmos资源存放位置"),
        new Tuple<bool, string, string>(false,"Plugins","一般 andoird 或者 ios 要接一些sdk，可以把sdk依赖的库文件放在这里。"),
        new Tuple<bool, string, string>(false,"Resources","Resources文件夹下的资源不管你用还是不用都会被打包。会压缩。"),
        new Tuple<bool, string, string>(false,"StreamingAssets","这个文件夹下的资源也会全都打包。不会压缩，只读文件夹。"),
        new Tuple<bool, string, string>(false,"Standard Assets","标准资源目录，放脚本会第一时间编译。"),
        new Tuple<bool, string, string>(false,"Pro Standard Assets","标准资源目录，放脚本会第一时间编译。Unity3D Pro使用。")
    };
    private int toolbarIndex;
    private static FolderManager Singleton;

    public static void ShowWindow()
    {
        Singleton = CreateInstance<FolderManager>();

        EditorWindow window = EditorWindow.GetWindow<FolderManager>();
        window.position = new Rect(100, 100, 500, 600);
        window.titleContent = new GUIContent("文件夹管理");
        window.Show();
    }
    
    private void CreateFolder(List<Tuple<bool, string, string>> folderinfo)
    {
        int folderCount = 0;
        for (int i = 0, count = folderinfo.Count; i < count; i++)
        {
            if (folderinfo[i].Item1)
            {
                string _folderpath = System.Environment.CurrentDirectory + "/Assets/" + folderinfo[i].Item2;
                if (!Directory.Exists(_folderpath))
                {
                    Directory.CreateDirectory(_folderpath);
                    folderCount++;
                }
            }
        }

        if (folderCount > 0)
        {
            ShowNotification(new GUIContent("成功创建" + folderCount + "个文件夹，请手动刷新Project视图"));
        }
        else
        {
            ShowNotification(new GUIContent("没有创建文件夹"));
        }
    }

    void OnGUI()
    {
        EditorGUILayout.HelpBox("标准文件创建工具\n统一各个项目的文件夹", MessageType.Warning, true);
        GUILayout.Space(3);
        
        EditorTools.Label("1.所有在Standard Assets、Pro Standard Assets或者Plugins文件夹中的脚本会产生一个Assembly-CSharp-filepass-vs.csproj文件，并且先编译；",Color.white,10, TextAnchor.MiddleLeft);
        EditorTools.Label("2.所有在Standard Assets/Editor、Pro Standard Assets/Editor或者Plugins/Editor文件夹中的脚本产生Assembly-CSharp-Editor-filepass-vs.csproj工程文件，接着编译；", Color.white, 10, TextAnchor.MiddleLeft);
        EditorTools.Label("3.所有在Assets/Editor外面的，并且不在(1),(2)中的脚本文件(一般这些脚本就是我们自己写的非编辑器扩展脚本)会产生Assembly-CSharp-vs.csproj工程文件，被编译；", Color.white, 10, TextAnchor.MiddleLeft);
        EditorTools.Label("4.所有在Assets/Editor中的脚本产生一个Assembly-CSharp-Editor-vs.csproj工程文件，被编译；", Color.white, 10, TextAnchor.MiddleLeft);

        toolbarIndex = GUILayout.Toolbar(toolbarIndex, new string[] { "Unity3D标准文件夹", "自定义文件夹" }, GUILayout.Height(25));
        GUILayout.Space(3);

        EditorGUILayout.BeginVertical(EditorStyles.textArea);
        {
            // 标准文件夹 
            if (toolbarIndex == 0)
            {
                // 自定义文件夹 
                for (int i = 0, count = standardFolderInfo.Count; i < count; i++)
                {
                    GUILayout.BeginHorizontal();
                    {
                        standardFolderInfo[i].Item1 = EditorGUILayout.Toggle(standardFolderInfo[i].Item2, standardFolderInfo[i].Item1, GUILayout.Width(200));
                        GUILayout.Label(standardFolderInfo[i].Item3);
                    }
                    GUILayout.EndHorizontal();
                }

                EditorGUILayout.BeginHorizontal();
                {
                    if (GUILayout.Button("反选", GUILayout.Height(20)))
                    {
                        for (int i = 0, count = standardFolderInfo.Count; i < count; i++)
                        {
                            standardFolderInfo[i].Item1 = !standardFolderInfo[i].Item1;
                        }
                    }
                    if (GUILayout.Button("创建文件夹", GUILayout.Height(20)))
                    {
                        CreateFolder(standardFolderInfo);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            else
            {
                // 自定义文件夹 
                for (int i = 0, count = customFolderInfo.Count; i < count; i++)
                {
                    GUILayout.BeginHorizontal();
                    {
                        customFolderInfo[i].Item1 = EditorGUILayout.Toggle(customFolderInfo[i].Item2, customFolderInfo[i].Item1, GUILayout.Width(200));
                        GUILayout.Label(customFolderInfo[i].Item3);
                    }
                    GUILayout.EndHorizontal();
                }

                EditorGUILayout.BeginHorizontal();
                {
                    if (GUILayout.Button("反选", GUILayout.Height(20)))
                    {
                        for (int i = 0, count = customFolderInfo.Count; i < count; i++)
                        {
                            customFolderInfo[i].Item1 = !customFolderInfo[i].Item1;
                        }
                    }
                    if (GUILayout.Button("创建文件夹", GUILayout.Height(20)))
                    {
                        CreateFolder(customFolderInfo);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
        }
        EditorGUILayout.EndVertical();
    }
}
